<template>
	<view>
		<view class="bg-f has-pdlr-20">
			<view class="z-input">
				<text>原密码</text>
				<input class="uni-input" password type="text" @input="onInput_oldpass" placeholder="请输入原密码" />
			</view>
			<view class="z-input">
				<text>请输入新密码</text>
				<input class="uni-input" password type="text" @input="onInput_newpass" placeholder="请输入新密码" />
			</view>
			<view class="z-input">
				<text>确认新密码</text>
				<input class="uni-input" password type="text" @input="onInput_compass" placeholder="请再次输入新密码" />
			</view>
			<view class="has-pd-12 is-red-color is-size-14">
				<rich-text :nodes="strings"></rich-text>
			</view>	
		</view>
		<view class="has-pd-20">
			<button type="primary" @click="savepass" class="z-yello">保存</button>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				foxtoken:'',
				oldpass:'',
				newpass:'',
				compass:'',
				nodes:[{
					name: 'div'
				}],
				strings: '',
				userinfo:[]
			}
		},
		onLoad() {
			let info = this.service.getfox();
			if(info[0]){
				this.foxtoken = info[0]['foxtoken'];
			}else{
				uni.reLaunch({
					url:'../logon/login'
				})
			}
			this.strings = '密码要求：可以包含数字、字母、下划线，并且要同时含有数字和字母，且长度要在8-16位之间';
		},
		methods:{
			onInput_oldpass:function(e){
				this.oldpass = e.detail.value;
			},
			onInput_newpass:function(e){
				this.newpass = e.detail.value;
			},
			onInput_compass:function(e){
				this.compass = e.detail.value;
			},
			savepass:function(){
				if(this.oldpass.length > 20 || this.oldpass.length <= 0){
					uni.showToast({
						title:'旧密码输入错误',
						icon:'none',
					})
					return;
				}
				if(this.service.checkpassword(this.newpass) === false){
					uni.showToast({
						title:'新密码不符合要求',
						icon:'none',
					})
					return;
				}
				if(this.newpass !== this.compass){
					uni.showToast({
						title:'新密码与确认密码不一致',
						icon:'none',
					})
					return;
				}
				uni.request({
					url:this.service.FOX_URL+'diamond/user_editpass',
					method:'POST',
					data: {
						user_key: this.foxtoken,
						old_pass: this.oldpass,
						new_pass: this.newpass
					},
					dataType:'json',
					header:{
						'content-type':'application/x-www-form-urlencoded',
					},
					success: res => {
						var rest = res.data;
						if(rest.status == 'error'){
							uni.showToast({
								title:rest.msg,
								icon:'none',
								mask:true
							})
						}else if(rest.status == 'success'){
							this.userinfo = rest.return;
							const data = {
							    foxtel: this.userinfo['user_name'],
							    foxcode: this.service.RandomNum(1000,9999),
								foxtoken: this.userinfo['user_key'],
							    foxactive: true
							}
							uni.clearStorageSync();
							this.service.addfox(data);
							uni.showToast({
								title:rest.msg,
								icon:'none',
								mask:true
							})
							setTimeout(function() {                           
							    uni.navigateTo({
							    	url:'../index/index'
							    })
							}, 1500)
						}
					}
				})
			}
		}
	}
</script>

<style>
	.z-input{
		border-bottom: 2rpx solid #E5E5E5;
		height: 100rpx;
		line-height: 100rpx;
	}
	.z-input:last-child{
		border-bottom: none;
	}
	.z-input text{
		float: left;
		width: 35%;
	}
	.z-input input{
		display: inline-block;
		float: left;
		margin-top: 26rpx;
	}
	.z-yello{
		background: #efb336;
	}
</style>
